package com.bonc.event.eventtype;

import java.util.Arrays;
import java.util.List;

import org.ansj.domain.Term;
import org.springframework.beans.factory.annotation.Autowired;

import com.bonc.event.extractor.Trigger;


/**
 * @author donggui@bonc.com.cn
 * @version 2016 2016年7月1日 上午11:55:18
 */
public class EventTypeFeatureExtractor {
	
	@Autowired
	ContextExtractor contextExtractor;
	
	@Autowired
	TriggerExtractor triggerExtractor;
	
	public EventTypeFeatureExtractor(){
		if(contextExtractor ==null){
			contextExtractor = new ContextExtractor();
		}
		if(triggerExtractor ==null){
			triggerExtractor = new TriggerExtractor();
		}
	}
	
	public String[] extract(String text){
		if(text!=null && !"".equals(text.trim())){
			String result = extract2String(text);
			return result.split("\t");
		}
		return null;		
	}
	
	public String extract2String(String text){
		
		List<Trigger> triggers = triggerExtractor.extract(text);
		System.out.println("triggers=="+ triggers.size());
		
		if(triggers!=null && triggers.size()>0){
			Trigger selectTigger = triggers.get(0);
			
			for(Trigger trigger:triggers){
				if(trigger.getLexicalCategory() == "Disaster"){
					selectTigger = trigger;
				}
				
				if(trigger.getLexicalCategory() == "Life_Injury" || 
						trigger.getLexicalCategory() == "Life_Die"){
					selectTigger = trigger;
					break;
				}
			}						
			
			System.out.println("trigger=="+selectTigger.getLiteral());
			String[] contexts = contextExtractor.extract(text, selectTigger.getLiteral());
			if(contexts !=null){
//				List<String> featureList = Arrays.asList(contexts);
//				System.out.println("featureList=="+featureList.size());
//				featureList.add(selectTigger.getLiteral());
				
				String result = selectTigger.getLiteral()+"\t";
				for(String feature:contexts){
					result += feature + '\t';
				}
				return result;
			}			
		}
		return null;		
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		String text = "2015年6月5日,美国丹佛遭冰雹灾害袭击,街道大批车辆被埋";

		EventTypeFeatureExtractor extractor = new EventTypeFeatureExtractor();
		
		String res = extractor.extract2String(text);
		
		System.out.println(res);
	}

}
